import { getChannelByCountryApi } from "@/api/common";
import { saveOrUpdateInsuranceCompanyApi } from "@/api/insuranceCompany";
import CommonHook from "@/hook/commonHook";
import { message } from "ant-design-vue";

export default function OperationHook(state: any) {
  const { formState, loading, formRef, emit, selectValue } = state;
  const { getClassifyArrayByFields } = CommonHook();

  const onSubmit = () => {
    formRef.value.validate().then(() => {
      const params = {
        ...formState,
        id: formState.id ? formState.id : undefined,
        goodLimitResList: formState.goodLimitResList.map((t: any) => ({
          ...t,
          channelOpts: undefined,
          channelIdList: t.channelIdList.map((q: any) => q[q.length - 1])
        }))
      };

      loading.value = true;
      saveOrUpdateInsuranceCompanyApi(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            emit("submit");
          }
        })
        .finally(() => {
          loading.value = false;
        });
    });
  };
  const resetForm = () => {
    formRef.value.resetFields();
    emit("update:visible", false);
  };

  // 导入文件
  const uploadRequestHandle = (e: any) => {
    formState.fileList = [e.file];
    e.onProgress({ percent: 100 });
  };
  const addHandle = () => {
    formState.goodLimitResList.push({
      countryId: "",
      goodValueLimit: 0
    });
  };
  const deleteHandle = (index: number) => {
    formState.goodLimitResList.splice(index, 1);
  };

  const getChennleHandle = async (e: string, row: any) => {
    loading.value = true;
    await getChannelByCountryApi(e)
      .then((t: any) => {
        row.channelOpts = getClassifyArrayByFields(t.data || [], "channelAllocation");
      })
      .finally(() => {
        loading.value = false;
      });
  };
  //根据国家选取渠道
  const channelOptsHandle = async (e: string, row: any) => {
    row.channelIdList = [];
    getChennleHandle(e, row);
  };

  //显示时禁用已选渠道
  const dropdownVisibleChange = (visible: boolean, row: InsuranceCompany.ChannelOptsType) => {
    if (!visible) return;
    //当前国家已禁用的渠道
    const currentValue: string[] = selectValue.value.get(row.countryId) || [];

    //获取已选的渠道
    const getValue = row.channelIdList?.map((t: any) => t[t.length - 1]);

    if (row.channelOpts) {
      loading.value = true;
      row.channelOpts?.map((q: any) => {
        q.children?.map((d: any) => {
          //当前渠道有值时，                                          禁用其他已选渠道                                   //当前渠道没值时
          d.disabled = (row.channelIdList.length && !getValue.includes(d.value) && currentValue.includes(d.value)) || (!row.channelIdList.length && currentValue.includes(d.value));
        });
      });
    }
    loading.value = false;
  };

  return {
    resetForm,
    onSubmit,
    uploadRequestHandle,
    addHandle,
    deleteHandle,
    channelOptsHandle,
    getChennleHandle,
    dropdownVisibleChange
  };
}
